using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public class EnemyTankManager : MonoBehaviour
{
    public int num = 0;
    [Header("初始敌人列表（初始为5个）")]
    public List<GameObject> enemyList = new List<GameObject>();

    [Header("可被实例化的新敌人预制体（3个）")]
    public List<GameObject> enemyPrefabs = new List<GameObject>();

    [Header("最终激活的两个指定对象")]
    public GameObject specifiedObject1;
    public GameObject specifiedObject2;

    private List<Vector3> initialPositions = new List<Vector3>();
    public int instantiateCount = 0;
    private int maxInstantiateCount = 8;
    public int enemydies = 0;
    public GameObject game;
    private void Start()
    {
        foreach (var enemy in enemyList)
        {
            initialPositions.Add(enemy.transform.localPosition);
        }
    }

    private void Update()
    {
        if (instantiateCount >= maxInstantiateCount)
        {
            if (enemyList.All(e => !e.activeSelf))
            {
                ActivateSpecifiedObjects();
            }
            return;
        }

        int rand = Random.Range(1, 4);
        if (rand == 2)
        {
            List<GameObject> disabled = enemyList.Where(e => !e.activeSelf).ToList();
            if (disabled.Count >= 2)
            {
                ReplaceDisabledEnemies(disabled);
            }
        }
    }
    public void CheckAllEnemiesDead()
    {
        // 确保列表不为空且所有坦克均被禁用
        bool allDead = enemyList.Count > 0 && enemyList.All(e => !e.activeSelf);
    
        if (allDead)
        {
            Debug.Log("所有敌方坦克已被玩家消灭！");
            game.SetActive(true);
        }
    }
    void ReplaceDisabledEnemies(List<GameObject> disabledEnemies)
    {
        if (enemyPrefabs.Count < 2) return;

        GameObject old1 = disabledEnemies[0];
        GameObject old2 = disabledEnemies[1];

        var script1 = old1.GetComponent<EnemyTankFollower>();
        var script2 = old2.GetComponent<EnemyTankFollower>();

        if (script1 == null || script2 == null) return;

        GameObject prefabA, prefabB;
        do
        {
            prefabA = enemyPrefabs[Random.Range(0, enemyPrefabs.Count)];
            prefabB = enemyPrefabs[Random.Range(0, enemyPrefabs.Count)];
        } while (prefabA == prefabB);

        GameObject instance1 = Instantiate(prefabA, transform);
        GameObject instance2 = Instantiate(prefabB, transform);

        instance1.SetActive(false);
        instance2.SetActive(false);
        instance1.transform.position = new Vector3(-13, 0, 35.62f);
        instance2.transform.position = new Vector3(-13, 0, 35.62f);

        int indexA = enemyList.IndexOf(old1);
        int indexB = enemyList.IndexOf(old2);

        if (indexA >= 0 && indexA < initialPositions.Count)
            instance1.transform.localPosition = initialPositions[indexA];

        if (indexB >= 0 && indexB < initialPositions.Count)
            instance2.transform.localPosition = initialPositions[indexB];

        var newScript1 = instance1.GetComponent<EnemyTankFollower>();
        var newScript2 = instance2.GetComponent<EnemyTankFollower>();

        if (newScript1 != null)
        {
            newScript1.forwardDistance = script1.forwardDistance;
            newScript1.initialTargetDistance = script1.initialTargetDistance;
            newScript1.horizontalOffset = script1.horizontalOffset;
            newScript1.enabled = true; // 显式激活脚本
        }

        if (newScript2 != null)
        {
            newScript2.forwardDistance = script2.forwardDistance;
            newScript2.initialTargetDistance = script2.initialTargetDistance;
            newScript2.horizontalOffset = script2.horizontalOffset;
            newScript2.enabled = true; // 显式激活脚本
        }

        int shellCount = Mathf.Min(instantiateCount + 1, 3);

        var shooter1 = instance1.GetComponent<AutoShellShooter>();
        if (shooter1 != null)
            shooter1.shellsPerShot = shellCount;

        var shooter2 = instance2.GetComponent<AutoShellShooter>();
        if (shooter2 != null)
            shooter2.shellsPerShot = shellCount;

        var health1 = instance1.GetComponent<EnemyHealth>();
        if (health1 != null)
            health1.maxHealth += instantiateCount * 50;

        var health2 = instance2.GetComponent<EnemyHealth>();
        if (health2 != null)
            health2.maxHealth += instantiateCount * 50;

        enemyList.Remove(old1);
        enemyList.Remove(old2);
        Destroy(old1);
        Destroy(old2);

        enemyList.Add(instance1);
        enemyList.Add(instance2);

        instance1.SetActive(true);
        instance2.SetActive(true);

        instantiateCount += 2;
    }

    void ActivateSpecifiedObjects()
    {
        if (specifiedObject1 != null)
        {
            specifiedObject1.SetActive(true);
            specifiedObject1.transform.SetParent(null);
        }

        if (specifiedObject2 != null)
        {
            specifiedObject2.SetActive(true);
            specifiedObject2.transform.SetParent(null);
        }

        Debug.Log("已实例化满10个对象且全部禁用，激活指定对象并解除父子关系");
    }
}
